package com.naiterui.ehp.bs.patient.dao.impl;

import com.naiterui.ehp.bs.patient.dao.IPatientFamilyDao;
import com.naiterui.ehp.bs.patient.domain.PatientFamily;
import com.naiterui.ehp.bs.patient.enums.FamilyRelationEnum;
import com.naiterui.ehp.bs.patient.repository.IApPatientFamilyRepository;
import com.naiterui.ehp.bs.patient.utils.exception.PatientBusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 家庭成员持久化实现层
 *
 * @since 2.18
 */
@Component
public class ApPatientFamilyDaoImpl implements IPatientFamilyDao {

    @Autowired
    private IApPatientFamilyRepository patientFamilyRepository;

    @Override
    public List<PatientFamily> getFamilyListByPatientId(Long patientId) throws PatientBusinessException {
        List<PatientFamily> familyList = patientFamilyRepository.findByPatientIdOrderByCreatedAtDescIdAsc(patientId);
        return familyList;
    }

    @Override
    public PatientFamily getSelfInfo(Long patientId) throws PatientBusinessException {
        Integer selfCode = FamilyRelationEnum.RELATION_ONESELF.getValue();
        return patientFamilyRepository.findFirstByPatientIdAndRelation(patientId, selfCode);
    }

    @Override
    public PatientFamily getFamilyByNameRelation(String name, Integer relation) throws PatientBusinessException {
        return patientFamilyRepository.findFirstByNameAndRelation(name, relation);
    }

    @Override
    public PatientFamily save(PatientFamily patientFamily) {
        return patientFamilyRepository.save(patientFamily);
    }

    @Override
    public PatientFamily get(Long id) {
        return patientFamilyRepository.get(id);
    }

    @Override
    public PatientFamily update(PatientFamily patientFamily) {
        return patientFamilyRepository.update(patientFamily);
    }
}
